package com.example.sbbsec;

import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by Arat.Wu on 2018/12/04.
 */
public class completableMethods6 {



    public static void main(String[] args) {
        final List<City> cities = newArrayList();
        cities.add(City.builder().id(1L).name("太仓").info("太仓信息").build());
        cities.add(City.builder().id(2L).name("沙溪").build());

        final List<City> cities1 = Arrays.asList(City.builder().id(1L).name("太仓").build(),
                City.builder().id(2L).name("沙溪").info("沙溪信息").build(),
                City.builder().id(3L).name("浏河").info("浏河信息").build());

        List<Integer> list3 = Arrays.asList(64, 49, 25, 16, 9, 4, 1, 81, 36, 36);
        List<Integer> list4 = newArrayList();
        Map<String,Integer> stringIntegerMap = Maps.newConcurrentMap();
        Map<String,Integer> stringIntegerMap2 = Maps.newConcurrentMap();
        Map<String,Integer> stringIntegerMap3 = Maps.newConcurrentMap();

        final List<Integer> collect = IntStream.range(1, 5).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        final ExecutorService executorService = Executors.newFixedThreadPool(4);
        Long start =System.currentTimeMillis();
        final CompletableFuture[] counts = collect.parallelStream().map(item ->
                CompletableFuture.runAsync(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    stringIntegerMap.merge("count", item, (v1, v2) -> v1 + v2);
                })
        ).toArray(CompletableFuture[]::new);

        final CompletableFuture[] counts1 = collect.parallelStream().map(item ->
                CompletableFuture.runAsync(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    stringIntegerMap2.merge("count", item, (v1, v2) -> v1 + v2);
                })
        ).toArray(CompletableFuture[]::new);

        final CompletableFuture[] counts2 = collect.parallelStream().map(item ->
                CompletableFuture.runAsync(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    stringIntegerMap3.merge("count", item, (v1, v2) -> v1 + v2);
                })
        ).toArray(CompletableFuture[]::new);

        CompletableFuture.allOf(counts).join();
        CompletableFuture.allOf(counts1).join();
        CompletableFuture.allOf(counts2).join();

        Long time=System.currentTimeMillis()-start;
        System.out.println(time);
       // CompletableFuture.runAsync()
    }


}
